package com.abbas.smartCity.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;

import com.abbas.smartCity.App;
import com.abbas.smartCity.R;
import com.abbas.smartCity.bean.gov.GovAppealCategoryTypeBean;
import com.abbas.smartCity.ui.govservice.AddAppealActivity;
import com.abbas.smartCity.ui.govservice.AppealActivity;
import com.bumptech.glide.Glide;
import com.youth.banner.adapter.BannerAdapter;

import java.util.List;

/**
 * 政府服务热线模块 诉求分类条目适配器
 * 要求左右滑动，一行4个，一页两行
 * 主要是借用Banner实现底部的指示器
 * 记得重写 getItemCount() getRealCount() 的返回数量
 */
public class GovBannerAdapter extends BannerAdapter<GovAppealCategoryTypeBean.RowsBean, GovBannerAdapter.ServerHolder> {

    public GovBannerAdapter(List<GovAppealCategoryTypeBean.RowsBean> data) {
        super(data);
    }

    @Override
    public int getItemCount() {
        return 2;
    }

    @Override
    public int getRealCount() {
        return 2;
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public ServerHolder onCreateHolder(ViewGroup viewGroup, int position) {
        final GridView gridView = new GridView(viewGroup.getContext());
        gridView.setBackgroundColor(R.color.gray_200);
        final ViewGroup.LayoutParams layoutParams = new ViewPager2.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
        gridView.setLayoutParams(layoutParams);
        return new ServerHolder(gridView);
    }

    @Override
    public void onBindView(ServerHolder serverHolder, GovAppealCategoryTypeBean.RowsBean rowsBean, int position, int i1) {
        serverHolder.gv.setNumColumns(4);
        //第一页
        if (position == 0){
            serverHolder.gv.setAdapter(new ListCommAdapter<GovAppealCategoryTypeBean.RowsBean,ListCommAdapter.ServerHolder>(
                    mDatas.subList(0,8),
                    ListCommAdapter.ServerHolder.class,
                    R.layout.server_item
            ) {
                @Override
                protected void show(GovAppealCategoryTypeBean.RowsBean data, View convertView, ServerHolder viewHolder, int position, Context context) {
                    Glide.with(context)
                            .load(App.getBaseUrl() + data.getImgUrl())
                            .into(viewHolder.im);
                    viewHolder.tv.setText(data.getName());
                }
            });
            serverHolder.gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    final GovAppealCategoryTypeBean.RowsBean bean = mDatas.get(i);
                    final Intent intent = new Intent(view.getContext(), AppealActivity.class);
                    intent.putExtra("id",bean.getId());
                    view.getContext().startActivity(intent);
                }
            });
        }else if(position == 1){//第二页
            serverHolder.gv.setAdapter(new ListCommAdapter<GovAppealCategoryTypeBean.RowsBean,ListCommAdapter.ServerHolder>(
                    mDatas.subList(8,16),
                    ListCommAdapter.ServerHolder.class,
                    R.layout.server_item
            ) {
                @Override
                protected void show(GovAppealCategoryTypeBean.RowsBean data, View convertView, ServerHolder viewHolder, int position, Context context) {
                    Glide.with(context)
                            .load(App.getBaseUrl() + data.getImgUrl())
                            .into(viewHolder.im);
                    viewHolder.tv.setText(data.getName());
                }
            });
            serverHolder.gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    if (i == 7){
                        view.getContext().startActivity(new Intent(view.getContext(),AddAppealActivity.class));
                    }else {
                        final GovAppealCategoryTypeBean.RowsBean bean = mDatas.get(8 + i);
                        final Intent intent = new Intent(view.getContext(), AppealActivity.class);
                        intent.putExtra("id",bean.getId());
                        view.getContext().startActivity(intent);
                    }
                }
            });
        }

    }


    /**
     * 全部服务持有者
     */
    public static class ServerHolder  extends RecyclerView.ViewHolder{
        public GridView gv;

        public ServerHolder(@NonNull GridView itemView) {
            super(itemView);
            gv = itemView;
        }
    }

}
